//
//  CN_HUD.swift
//  cn
//
//  Created by Mac on 2021/1/11.
//  Copyright © 2021 szq. All rights reserved.
//

import UIKit
//import Lottie
import MBProgressHUD


open class CN_HUD {
    
    static let shared = CN_HUD()
    
    private init() {}

//    public var animationView: AnimationView?
    
    private let window = UIApplication.shared.windows.first { $0.isKeyWindow }
    
    /// MARK: window 上
//    func showLoading_Animation(animation: String) {
//        animationView = AnimationView(name: animation)
//        let hud = MBProgressHUD.showAdded(to: window!, animated: true)
//        hud.mode = .customView
//        hud.bezelView.alpha = 0
//        hud.graceTime = 2
//        hud.removeFromSuperViewOnHide = true
//        animationView!.frame.size = CGSize(width: cn_scale(130), height: cn_scale(130))
//        animationView!.center = hud.center
//        animationView!.contentMode = .scaleAspectFill
//        animationView!.loopMode = .loop
//        animationView!.animationSpeed = 1.0
//        hud.addSubview(animationView!)
//        hud.show(animated: true)
//        animationView!.play()
//    }
//    func hideLoading_Animation() {
//        MBProgressHUD.hide(for: (window!), animated: true)
//        animationView!.stop()
//    }
    
    func showLoading(backgroundColor: UIColor, color: UIColor, delay: Int) {
                
        let hud = MBProgressHUD.showAdded(to: (window!), animated: true)
        hud.mode = .indeterminate
        hud.contentColor = color
        hud.label.text = "加载中"
        hud.label.textColor = color
        hud.bezelView.style = .solidColor
        hud.bezelView.color = color
        hud.bezelView.backgroundColor = backgroundColor
        hud.margin = 12.0
        hud.removeFromSuperViewOnHide = true
        
//        UIActivityIndicatorView.appearanceWhenContained(within: [MBProgressHUD.self])?.color = color
        
        // Setting the color of the activity indicator directly
            if let activityIndicator = hud.customView as? UIActivityIndicatorView {
                activityIndicator.color = color
            } else {
                let activityIndicator = UIActivityIndicatorView(style: .medium) // Choose the appropriate style
                activityIndicator.color = color
                hud.customView = activityIndicator
            }
        hud.show(animated: true)
        hud.hide(animated: true, afterDelay: TimeInterval(delay))
    }
    func hideLoading() {
        
        MBProgressHUD.hide(for: (window!), animated: true)
    }
    
    func showTips(backgroundColor: UIColor, title: String, delay: Double, titleColor: UIColor) {
        let hud = MBProgressHUD.showAdded(to: window!, animated: true)
        hud.bezelView.style = .solidColor
        hud.animationType = MBProgressHUDAnimation.zoomOut
        hud.mode = MBProgressHUDMode.text
        hud.bezelView.color = backgroundColor
        hud.label.font = UIFont.cn_fontSize(ofSize: 14)
        hud.label.textColor = titleColor
        hud.label.numberOfLines = 0
        hud.label.text = title
        hud.margin = 12.0
        hud.removeFromSuperViewOnHide = true
        hud.show(animated: true)
        hud.hide(animated: true, afterDelay: TimeInterval(delay))
        
    }
    func showLogngTips(backgroundColor: UIColor, title: String, delay: Double, titleColor: UIColor) {
        let hud = MBProgressHUD.showAdded(to: window!, animated: true)
        hud.bezelView.style = .solidColor
        hud.animationType = MBProgressHUDAnimation.zoomOut
        hud.mode = MBProgressHUDMode.text
        hud.bezelView.color = backgroundColor
        hud.label.font = UIFont.cn_fontSize(ofSize: 14)
        hud.label.textColor = titleColor
        hud.label.numberOfLines = 0
        hud.label.text = title
        hud.margin = 12.0
        hud.removeFromSuperViewOnHide = true
        hud.show(animated: true)
        hud.hide(animated: true, afterDelay: TimeInterval(delay))
        
    }

    /// 进度条
    func showProgress(backgroundColor: UIColor, title: String, progress: Float, titleColor: UIColor) {
        let hud = MBProgressHUD.showAdded(to: window!, animated: true)
        hud.bezelView.style = .solidColor
        hud.animationType = MBProgressHUDAnimation.zoomOut
        hud.mode = MBProgressHUDMode.text
        hud.bezelView.color = backgroundColor
        hud.label.font = UIFont.cn_fontSize(ofSize: 14)
        hud.label.textColor = titleColor
        hud.label.numberOfLines = 0
        hud.label.text = title
        hud.margin = 12.0
        hud.removeFromSuperViewOnHide = true
        hud.show(animated: true)
        if progress > 0.99 {
            hud.hide(animated: true)
        }
    }
}

public extension UIView {
    
//    func showLoading_Animation(animation: String = "ice_cream_animation") {
//        CN_HUD.shared.showLoading_Animation(animation: animation)
//    }
//    func hideLoading_Animation() {
//        CN_HUD.shared.hideLoading_Animation()
//    }
    
    func showLoading(backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.7), color: UIColor = UIColor.white, delay: Int = 10) {
        
        CN_HUD.shared.showLoading(backgroundColor: backgroundColor, color: color, delay: delay)
    }
    func hideLoading() {
        CN_HUD.shared.hideLoading()
    }
    
    func showTips(backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.7), title: String, delay: Double = 2.0, titleColor: UIColor = UIColor.white) {
        CN_HUD.shared.hideLoading()
        CN_HUD.shared.showTips(backgroundColor: backgroundColor, title: title, delay: delay, titleColor: titleColor)
    }
    func showLongTips(backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.7), title: String, delay: Double = 4.0, titleColor: UIColor = UIColor.white) {
        CN_HUD.shared.hideLoading()
        CN_HUD.shared.showLogngTips(backgroundColor: backgroundColor, title: title, delay: delay, titleColor: titleColor)
    }
    
    /// 进度条
    func showProgress(backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.7), progress: Float, title: String = "视频处理中...", titleColor: UIColor = UIColor.white) {
        CN_HUD.shared.hideLoading()
        CN_HUD.shared.showProgress(backgroundColor: backgroundColor, title: title, progress: progress, titleColor: titleColor)
    }
    /// 当网络请求进入 .erro时，使用
    func showError(error: Error) {
        Macro_Network.shared.canConnectNetwork { (canConnectNetwork) in
            DispatchQueue.main.async {
                if canConnectNetwork == true {
                    self.showTips(backgroundColor: UIColor.black, title: error.localizedDescription)
                } else {
                    self.showTips(backgroundColor: UIColor.black, title: "网络已断开，请检查网络连接")
                }
            }
            
        }
    }
    
}
